package com.my;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Collection;

/**
 * Created by YJH on 2017/7/7 0:27.
 */
public class ForEachTag extends SimpleTagSupport {

    private Collection<?> items;
    private String var;

    @Override
    public void doTag() throws JspException, IOException {
        //1.遍历items对应的集合
        if (items != null) {
            for (Object obj : items) {
                //2.把正在遍历的对象放到pageContext中，键：var，值：正在遍历的对象
                getJspContext().setAttribute(var, obj);
                //3.把标签体的内容直接输入到页面上
                getJspBody().invoke(null);
            }
        }
    }

    public void setItems(Collection<?> items) {
        this.items = items;
    }

    public void setVar(String var) {
        this.var = var;
    }
}
